<template>
  <div class="relative w-full h-full overflow-y-auto">
    <PageHeader :title="$t('order_lists_detail.订单详情')">
      <template #right>
        <el-button type="primary" @click="goBack">
          <SvgIcon name="iconfanhui" color="#fff" class="mr4"></SvgIcon
          >{{ $t("order_lists_detail.返回上级页面") }}</el-button
        >
      </template>
    </PageHeader>
    <Step @refresh="getDetail" :detail="order_lists_detail" v-show="order_lists_detail.id" />

    <div class="mt15 mb15 pl5 color-333333 font-700">
      {{ $t("order_lists_detail.订单信息") }}:
    </div>
    <el-table
      :show-header="false"
      :data="tableData"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
      :span-method="arraySpanMethod"
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1" />
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>
    <div class="mt15 mb15 pl5 color-333333 font-700">
      {{ $t("order_lists_detail.商品信息") }}:
    </div>
    <el-table
      :data="order_lists_detail.saleSkus"
      style="width: 100%"
      border
      class="cover-table"
      :span-method="objectSpanMethod"
    >
      <el-table-column
        :label="$t('order_lists_detail.序号')"
        width="100"
        type="index"
      />
      <el-table-column prop="info" :label="$t('order_lists_detail.商品信息')">
        <template #default="{ row }">
          <GoodsTableTd :row="row" />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('order_lists.小料')"
        width="200"
      >
        <template #default="{ row }">
          <div class="border-left-right">
            <div>{{$t('currency.￥')}}{{row.payRealMate}}</div>
            <div class="color-999999">
              {{row.skuMateDetail.map(v=> v['mateName' + locale]).join(',')}}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="payReal"
        :label="$t('order_lists_detail.实付金额')"
        width="160"
        :formatter="(row) => $t('currency.￥') + row.payReal"
      />
<!--      <el-table-column
        prop="payRealPackingFee"
        :label="$t('order_lists_detail.包装费')"
        width="160"
      />-->
    </el-table>
    <div class="order_detail_total">
      <span class="amount_total">
        {{ $t("order_lists_detail.订单实付金额") }}({{$t('currency.￥')}}{{ order_lists_detail.payReal }}) = 
        {{ $t("order_lists.小料金额") }}({{$t('currency.￥')}}{{ order_lists_detail.payRealMate }}) +
        {{ $t("order_lists.菜品金额") }}({{$t('currency.￥')}}{{ order_lists_detail.payRealSku }}) +
        {{ $t("order_lists.小费金额") }}({{$t('currency.￥')}}{{ order_lists_detail.payRealTip }})  +
        {{ $t("order_lists.包装费用") }}({{$t('currency.￥')}}{{ order_lists_detail.payRealPackingFee }})
      </span>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, watch, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import Step from "./components/step.vue";
import { useI18n } from "vue-i18n";
import Api from "@/config/api/api";
import GoodsTableTd from "@/components/goodsTableTd.vue";

const { locale, t } = useI18n();
const router = useRouter();
const route = useRoute();
const order_lists_detail = ref({});
const tableData = ref([
  // {
  //   label1: "订单类型",
  //   value1: "普通订单",
  //   label2: "订单号",
  //   value2: order_lists_detail.saleNo,
  // },
  {
    label1: t("order_lists_detail.店铺名称"),
    value1: order_lists_detail["tenantName" + locale.value],
    label2: t("order_lists_detail.订单号"),
    value2: order_lists_detail.saleNo,
    // label2: "支付方式",
    // value2: "在线支付",
  },
  // {
  //   label1: "订单备注",
  //   value1: "--",
  // },
]);
const tableData2 = ref([
  {
    label1: t("order_lists_detail.会员名称"),
    value1: order_lists_detail.vipNickName,
    label2: t("order_lists_detail.收货人"),
    value2: order_lists_detail.vipAddressName,
  },
  {
    label1: t("order_lists_detail.收货人手机号"),
    value1: order_lists_detail.vipAddressPhone,
    label2: t("order_lists_detail.收货地址"),
    value2:
      order_lists_detail.value["vipAddressProvince" + locale.value] +
      order_lists_detail.value["vipAddressCity" + locale.value] +
      order_lists_detail.value["vipAddressArea" + locale.value] +
      order_lists_detail.value.vipAddressAddress +
      order_lists_detail.value.vipAddressDetail,
  },
]);

const tableData3 = ref([
  {
    label1: t("order_lists_detail.是否申请开票"),
    value1:
      order_lists_detail.value.vipInvoiceStatus === 0
        ? t("order_lists_detail.否")
        : t("order_lists_detail.是"),
    label2: t("order_lists_detail.申请时间"),
    value2: order_lists_detail.value.vipInvoiceTime,
  },
  {
    label1: t("order_lists_detail.发票抬头"),
    value1: order_lists_detail.value.vipInvoiceHeader,
    label2: t("order_lists_detail.纳税人识别号"),
    value2: order_lists_detail.value.vipInvoiceTaxpayerNo,
  },
  {
    label1: t("order_lists_detail.联系人"),
    value1: order_lists_detail.value.vipInvoiceContact,
    label2: t("order_lists_detail.联系电话"),
    value2: order_lists_detail.value.vipInvoicePhone,
  },
  {
    label1: t("order_lists_detail.地址"),
    value1: order_lists_detail.value.vipInvoiceAddress,
    label2: t("order_lists_detail.邮箱"),
    value2: order_lists_detail.value.vipInvoiceEmail,
  },
  {
    label1: t("order_lists_detail.发票审核时间"),
    value1: order_lists_detail.value.vipInvoiceCheckTime,
    label2: t("order_lists_detail.审核失败原因"),
    value2: order_lists_detail.value.vipInvoiceCheckMessage,
  },
  {
    label1: t("order_lists_detail.发票图片"),
    value1: order_lists_detail.value.vipInvoiceCheckImage,
  },
]);
const hasSend = ref(false);
const tableData4 = ref([
  {
    label1: t("order_lists_detail.快递公司"),
    value1: order_lists_detail.value.sendName,
    label2: t("order_lists_detail.快递单号"),
    value2: order_lists_detail.value.sendNo,
  },
]);

watch(
  () => locale.value,
  (newVal) => {
    console.log(newVal);
    tableData.value[0].value1 = order_lists_detail["tenantName" + newVal];
    // 更新tableData中的标签
    tableData.value[0].label1 = t("order_lists_detail.店铺名称");
    tableData.value[0].label2 = t("order_lists_detail.订单号");
    tableData2.value[0].label1 = t("order_lists_detail.会员名称");
    tableData2.value[0].label2 = t("order_lists_detail.收货人");
    tableData2.value[1].label1 = t("order_lists_detail.收货人手机号");
    tableData2.value[1].label2 = t("order_lists_detail.收货地址");
    tableData3.value[0].label1 = t("order_lists_detail.是否需要开票");
    tableData3.value[0].value1 = t("order_lists_detail.否");
    tableData4.value[0].label1 = t("order_lists_detail.快递公司");
    tableData4.value[0].label2 = t("order_lists_detail.快递单号");
  }
);

const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  // const length = order_lists_detail.value.saleSkus.length;
  // if (columnIndex === 1 && rowIndex === length - 1) {
  //   return [1, length];
  // } else {
    return [1, 1];
  // }
};

const step = ref(1);
const goBack = () => {
  router.back();
};

const cellClassName = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex % 2 === 0) {
    return "warning-column winth-20";
  } else {
    return "winth-30";
  }
};

const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (
    (columnIndex === 2 && rowIndex === 2) ||
    (columnIndex === 3 && rowIndex === 2)
  ) {
    return [0, 0];
  } else if (columnIndex === 1 && rowIndex === 2) {
    return [1, 3];
  } else {
    return [1, 1];
  }
};

const getDetail = () => {
  Api.getOrderDetail(route.query.id).then((res) => {
    order_lists_detail.value = res.data || {};

    tableData.value[0].value1 = res.data["tenantName" + locale.value];
    tableData.value[0].value2 = res.data["saleNo"];
    tableData2.value[0].value1 = res.data["vipNickName"];
    tableData2.value[0].value2 = res.data["vipAddressName"];
    tableData2.value[1].value1 = res.data["vipAddressPhone"];
    tableData2.value[1].value2 =
      res.data["vipAddressProvince" + locale.value] +
      res.data["vipAddressCity" + locale.value] +
      res.data["vipAddressArea" + locale.value] +
      res.data.vipAddressAddress +
      res.data.vipAddressDetail;
    hasSend.value = res.data["sendTime"] !== null;
    tableData4.value[0].value1 = res.data["sendName"];
    tableData4.value[0].value2 = res.data["sendNo"];
  });
};

onMounted(() => {
  getDetail();
});
</script>

<style lang="scss" scoped>
.mt50 {
  margin-top: 50px;
}

:deep(.warning-column) {
  background-color: #f8f9fa;
}
:deep(.winth-20) {
  width: 20%;
}
:deep(.winth-30) {
  width: 30%;
}
.w-380 {
  width: 380px;
}
.cover-table {
  .product-img {
    width: 100px;
    height: 100px;
    object-fit: cover;
  }
}

.order_detail_total {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  margin-top: 10px;
  margin-bottom: 20px;
  padding-right: 5px;
  font-size: 14px;
  font-weight: 700;
  height: 45px;
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 0 8px #9999990d, 0 0 8px #9999990d;
  .amount_total {
    color: #ff2b4e;
  }
}
</style>
